/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.bean;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import toesp.entity.Grouppermission;

/**
 *
 * @author satria.sp
 */
public class GroupBean {
    private EntityManagerFactory emf;

    public GroupBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public boolean add(Grouppermission Grouppermission) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null ;
        
        try { 
            EntityManager em = getEntityManager();
            if(this.getOnebyId(Grouppermission.getGroupId())==null){
                em.getTransaction().begin();
                em.persist(Grouppermission);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {  
            tx.rollback(); 
        }finally{
            //this.getCloseEntityManager();
        }
        
        return bolRet; 
    }
    
    public boolean edit(Grouppermission Grouppermission) throws IllegalStateException, SystemException{
        Transaction tx = null;
        boolean bolRet = false; 
        
        try {
            EntityManager em = getEntityManager();
            if(this.getOnebyId(Grouppermission.getGroupId())!=null){
                em.getTransaction().begin();
                em.merge(Grouppermission);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public boolean del(String p_intGuid) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null; 
        
        try {
            EntityManager em = getEntityManager();
            Grouppermission Grouppermission = this.getOnebyId(p_intGuid);
            if(Grouppermission!=null){
                em.getTransaction().begin();
                em.remove(Grouppermission);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public Grouppermission getOnebyId(String p_intGuid) throws Exception {
        Grouppermission o = null;
        try {              
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Grouppermission o WHERE o.groupId=:p_intGuid");   
            q.setParameter("p_intGuid", p_intGuid);            
            List<Grouppermission> lstPg = q.getResultList();
            o = (Grouppermission) lstPg.get(0);
        } catch (Exception e) {            
        }finally{   
            //this.getCloseEntityManager();
        }  
        return o;
    }  
    
    public List<Grouppermission> getAll() throws Exception { 
        List<Grouppermission> lstGroup = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Grouppermission o ORDER BY o.groupName ASC");     
            lstGroup = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstGroup;
    }
    
    public int count() {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT COUNT(o) FROM Grouppermission o");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    public Grouppermission getOnebyId(int p_intGroupId) throws Exception {
        Grouppermission g = null;
         
        try {
            EntityManager em = getEntityManager();
            Query  q = em.createQuery("SELECT o FROM Grouppermission o WHERE o.groupId=:p_intGroupId");   
            q.setParameter("p_intGroupId", p_intGroupId);            
            List<Grouppermission> lstPg = q.getResultList();
            g = (Grouppermission) lstPg.get(0); 
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return g;
    }
    
    public List<Grouppermission> getPaging(int pintStart, int pintMax) throws Exception {
        List<Grouppermission> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Grouppermission o ORDER BY o.groupName ASC");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public int countFind(String p_strName) {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT count(o) FROM Grouppermission o WHERE o.groupName LIKE :p_strName ORDER BY o.groupName ASC");
            q.setParameter("p_strName","%"+p_strName+"%");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    public List<Grouppermission> getFindByNamePaging(String p_strName,int pintStart, int pintMax) throws Exception {
        List<Grouppermission> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Grouppermission o WHERE o.groupName LIKE :p_strName ORDER BY o.groupName ASC");
            q.setParameter("p_strName","%"+p_strName+"%");
            q.setFirstResult(pintStart);
            q.setMaxResults(pintMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
}
